package com.tiantian.service.impl;

import com.tiantian.mapper.CourseMapper;
import com.tiantian.pojo.Course;
import com.tiantian.service.CourseService;
import com.tiantian.utils.UserHolder;
import com.tiantian.utils.vo.ClassNameAndInstitutionVO;
import com.tiantian.utils.vo.ClassNameAndValueKeyWordVO;
import com.tiantian.utils.vo.UserClassAndProgressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Component
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> getClassList(Integer uid, String className, String institution) {
        return courseMapper.getClassList(uid, className, institution);
    }

    @Override
    public int updateById(Course course) {
        course.setUid(UserHolder.getUid());
        course.setModifyTime(new Date());
        return courseMapper.updateById(course);
    }

    @Override
    public List<Course> getById(Integer id) {
        return courseMapper.getById(id, UserHolder.getUid());
    }

    @Override
    public int deleteById(Integer id) {
        return courseMapper.delById(id, UserHolder.getUid());
    }

    @Override
    public int addCourse(Course course) {
        course.setModifyTime(new Date());
        return courseMapper.insertCourse(course);
    }

    @Override
    public List<UserClassAndProgressVO> selectUserClassAndProgress(Integer uid) {
        return courseMapper.selectUserClassAndProgress(uid);
    }


    /**
     * 词云图所需数据
     */

    @Override
    public List<ClassNameAndValueKeyWordVO> wordCountByProgress(Integer uid) {
        List<ClassNameAndInstitutionVO> data = courseMapper.selectClassNameAndInstitutionsByUserId(uid);
        List<ClassNameAndValueKeyWordVO> res = new ArrayList<>();
        for (ClassNameAndInstitutionVO d : data) {
            String molecule = d.getProgress().split("/")[0];
            String denominator = d.getProgress().split("/")[1];
            ClassNameAndValueKeyWordVO institutionAndValue = new ClassNameAndValueKeyWordVO(d.getInstitution(), Integer.parseInt(molecule));
            ClassNameAndValueKeyWordVO classNameAndValue = new ClassNameAndValueKeyWordVO(d.getClassName(), Integer.parseInt(denominator));
            res.add(institutionAndValue);
            res.add(classNameAndValue);
        }
        return res;
    }

    @Override
    public List<UserClassAndProgressVO> getFollowUserClassAndProgress(Integer uid) {
        return courseMapper.getFollowUserClassAndProgress(uid);
    }


}
